#!/usr/bin/env python
"""
urlcheck.py will spawn this script for every
domain that doesn't work.

"""
import os
import urllib2
import time
import optparse
from mailer import mailer
import dbwork

def errors(website):
	try:
		urllib2.urlopen(website)
	except urllib2.HTTPError, e:
		return True

	except urllib2.URLError, e:
		return True


def delayed_check(website):
	time.sleep(25)
	if errors(website) is True:
		delayed_check(website)
	else:
		text_body = "The following website is now running properly: %s" % website
		subj = "%s is OK" % website
		dbwork.delete(website)
		notify(text_body, subj)

def notify(text_body, subj):
	""" Sends an email with information """
	user = ""
	passwd = ""
	to = [""]
	subject = subj
	text = text_body
	mailer(user, passwd, to, subject, text)

def main():
	p = optparse.OptionParser()
	options, args = p.parse_args()
	
	if len(args) == 1:
		website = args[0]
		delayed_check(website)
	else:
		p.print_help()

if __name__ == '__main__':
	main()
		

